package com.lpf.json.fastjson.ParseProcess;

import java.lang.reflect.Type;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider;

public class TestExtraTypeProvider {

	public static void main(String[] args) {

		ExtraProcessor processor = new MyExtraProcessor();

		VO vo = JSON.parseObject("{\"id\":123,\"value\":\"123456\"}", VO.class, processor);

		System.out.println("vo.getId() = " + vo.getId());
		System.out.println("vo.getAttributes().get(\"value\") = " + vo.getAttributes().get("value"));
		System.out.println("vo.getAttributes().get(\"value\").getClass() = " + vo.getAttributes().get("value").getClass());
		// value本应该是字符串类型的，通过getExtraType的处理变成Integer类型了。
	}

}

class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
	public void processExtra(Object object, String key, Object value) {
		VO vo = (VO) object;
		vo.getAttributes().put(key, value);
	}

	public Type getExtraType(Object object, String key) {

		System.out.println("---------------object = " + object);
		System.out.println("---------------key = " + key);
		System.out.println();

		if ("value".equals(key)) {
			return int.class;
		}
		return null;
	}
};